/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * VICliente.java
 *
 * Created on Aug 1, 2009, 9:25:51 AM
 */

package pe.edu.cibertec.reservas.view.desk;

import java.sql.Date;
import java.util.ArrayList;
import javax.swing.table.AbstractTableModel;
import pe.edu.cibertec.reservas.bean.Cliente;
import pe.edu.cibertec.reservas.bean.Empresa;
import pe.edu.cibertec.reservas.bean.Persona;
import pe.edu.cibertec.reservas.control.GestorCliente;
import pe.edu.cibertec.reservas.dao.interfaces.ClienteDAO;

/**
 *
 * @author Student
 */
public class frmCliente extends javax.swing.JInternalFrame {

    /** Creates new form VICliente */
    private int intOpcion;
    private static int NUEVO = 1;
    private static int MODIFICAR = 2;
    public frmCliente() {
        super("Mantenimiento de Clientes",false,true,false,true);
        initComponents();
        btnGroup.add(rbtnEmpresa);
        btnGroup.add(rbtnPersona);
        refrescarGrid();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnGroup = new javax.swing.ButtonGroup();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        txtCodigo = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        txtDireccion = new javax.swing.JTextField();
        txtTelefono = new javax.swing.JTextField();
        txtEmail = new javax.swing.JTextField();
        rbtnPersona = new javax.swing.JRadioButton();
        rbtnEmpresa = new javax.swing.JRadioButton();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        txtNombre = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        txtNroDoc = new javax.swing.JTextField();
        txtFechaNac = new javax.swing.JTextField();
        cmbSexo = new javax.swing.JComboBox();
        txtWeb = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        txtApellidoPat = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        txtApellidoMat = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tblClientes = new javax.swing.JTable();
        jMenuBar1 = new javax.swing.JMenuBar();
        mnNuevo = new javax.swing.JMenu();
        mnGrabar = new javax.swing.JMenu();
        mnEditar = new javax.swing.JMenu();
        mnEliminar = new javax.swing.JMenu();

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        setTitle("Mantenimiento de Clientes");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Cliente"));

        jLabel1.setText("Código:");

        jLabel2.setText("Dirección:");

        jLabel3.setText("Teléfono:");

        txtCodigo.setEnabled(false);

        jLabel4.setText("Email:");

        rbtnPersona.setSelected(true);
        rbtnPersona.setText("Persona");

        rbtnEmpresa.setText("Empresa");

        jLabel5.setText("Nombre:");

        jLabel6.setText("NroDoc:");

        jLabel7.setText("Feha Nac.:");

        jLabel8.setText("Sexo:");

        jLabel9.setText("Pagina Web:");

        cmbSexo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "F", "M" }));
        cmbSexo.setToolTipText("sexo");

        jLabel10.setText("Apellido Pat.:");

        jLabel11.setText("Apellido Mat.:");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(12, 12, 12)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(txtCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(48, 48, 48)
                        .addComponent(rbtnPersona)
                        .addGap(49, 49, 49)
                        .addComponent(rbtnEmpresa))
                    .addComponent(txtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, 148, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(128, Short.MAX_VALUE))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel10)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtApellidoPat, javax.swing.GroupLayout.PREFERRED_SIZE, 148, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(232, 232, 232))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel7, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel6, javax.swing.GroupLayout.Alignment.TRAILING))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtNroDoc, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtFechaNac, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 64, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel8, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel9, javax.swing.GroupLayout.Alignment.TRAILING))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtWeb, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cmbSexo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(37, 37, 37))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtTelefono, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(339, Short.MAX_VALUE))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtDireccion, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabel4)
                .addGap(18, 18, 18)
                .addComponent(txtEmail, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(97, Short.MAX_VALUE))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel11)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtApellidoMat, javax.swing.GroupLayout.PREFERRED_SIZE, 148, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(230, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(rbtnPersona)
                    .addComponent(rbtnEmpresa))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(txtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel10)
                    .addComponent(txtApellidoPat, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 7, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel11)
                    .addComponent(txtApellidoMat, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtDireccion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addComponent(txtEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtTelefono, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(48, 48, 48)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(txtNroDoc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8)
                    .addComponent(cmbSexo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(txtFechaNac, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9)
                    .addComponent(txtWeb, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        tblClientes.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tblClientes.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        tblClientes.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblClientesMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(tblClientes);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 425, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(23, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 185, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(78, Short.MAX_VALUE))
        );

        mnNuevo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/pe/edu/cibertec/reservas/view/desk/images/group_blue_new.png"))); // NOI18N
        mnNuevo.setToolTipText("Nuevo");
        mnNuevo.setDisabledIcon(new javax.swing.ImageIcon(getClass().getResource("/pe/edu/cibertec/reservas/view/desk/images/group_grey_new.png"))); // NOI18N
        mnNuevo.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                mnNuevoMouseClicked(evt);
            }
        });
        mnNuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnNuevoActionPerformed(evt);
            }
        });
        jMenuBar1.add(mnNuevo);

        mnGrabar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/pe/edu/cibertec/reservas/view/desk/images/group_blue_add.png"))); // NOI18N
        mnGrabar.setToolTipText("Grabar");
        mnGrabar.setDisabledIcon(new javax.swing.ImageIcon(getClass().getResource("/pe/edu/cibertec/reservas/view/desk/images/group_grey_add.png"))); // NOI18N
        mnGrabar.setEnabled(false);
        mnGrabar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                mnGrabarMouseClicked(evt);
            }
        });
        mnGrabar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnGrabarActionPerformed(evt);
            }
        });
        jMenuBar1.add(mnGrabar);

        mnEditar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/pe/edu/cibertec/reservas/view/desk/images/group_blue_edit.png"))); // NOI18N
        mnEditar.setDisabledIcon(new javax.swing.ImageIcon(getClass().getResource("/pe/edu/cibertec/reservas/view/desk/images/group_grey_edit.png"))); // NOI18N
        mnEditar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                mnEditarMouseClicked(evt);
            }
        });
        mnEditar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnEditarActionPerformed(evt);
            }
        });
        jMenuBar1.add(mnEditar);

        mnEliminar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/pe/edu/cibertec/reservas/view/desk/images/group_blue_remove.png"))); // NOI18N
        mnEliminar.setToolTipText("Eliminar");
        mnEliminar.setDisabledIcon(new javax.swing.ImageIcon(getClass().getResource("/pe/edu/cibertec/reservas/view/desk/images/group_grey_remove.png"))); // NOI18N
        mnEliminar.setEnabled(false);
        mnEliminar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                mnEliminarMouseClicked(evt);
            }
        });
        mnEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnEliminarActionPerformed(evt);
            }
        });
        jMenuBar1.add(mnEliminar);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(19, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tblClientesMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblClientesMouseClicked
                 SeleccionarRegistro();
    }//GEN-LAST:event_tblClientesMouseClicked

    private void mnEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnEliminarActionPerformed
                GestorCliente.delete((Integer.parseInt(txtCodigo.getText())));
                refrescarGrid();
                limpiarText();
    }//GEN-LAST:event_mnEliminarActionPerformed

    private void mnGrabarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnGrabarActionPerformed
        if (intOpcion == NUEVO) {
        if (rbtnPersona.isSelected()){
        GestorCliente.add(new Persona(0,
                                      Integer.parseInt(txtNroDoc.getText()),
                                      txtApellidoPat.getText(),
                                      txtApellidoMat.getText(),
                                      txtNombre.getText(),
                                      Date.valueOf(txtFechaNac.getText()),
                                      cmbSexo.getSelectedItem().toString().charAt(0),
                                      txtDireccion.getText(),
                                      txtEmail.getText(),
                                      Integer.parseInt(txtTelefono.getText())));


         }else{
               GestorCliente.add(new Empresa(0,
                                      Integer.parseInt(txtNroDoc.getText()),
                                      txtNombre.getText(),
                                      txtWeb.getText(),
                                      txtDireccion.getText(),
                                      txtEmail.getText(),
                                      Integer.parseInt(txtTelefono.getText())));

         }
        }
        else {
        if (rbtnPersona.isSelected()){
        GestorCliente.update(new Persona(Integer.parseInt(txtCodigo.getText()),
                                      Integer.parseInt(txtNroDoc.getText()),
                                      txtApellidoPat.getText(),
                                      txtApellidoMat.getText(),
                                      txtNombre.getText(),
                                      Date.valueOf(txtFechaNac.getText()),
                                      cmbSexo.getSelectedItem().toString().charAt(0),
                                      txtDireccion.getText(),
                                      txtEmail.getText(),
                                      Integer.parseInt(txtTelefono.getText())));

         }else {
               GestorCliente.update(new Empresa(Integer.parseInt(txtCodigo.getText()),
                                      Integer.parseInt(txtNroDoc.getText()),
                                      txtNombre.getText(),
                                      txtWeb.getText(),
                                      txtDireccion.getText(),
                                      txtEmail.getText(),
                                      Integer.parseInt(txtTelefono.getText())));
         }

        }
        refrescarGrid();
        limpiarText();

    }//GEN-LAST:event_mnGrabarActionPerformed

    private void mnNuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnNuevoActionPerformed
        intOpcion = NUEVO;
        limpiarText();
    }//GEN-LAST:event_mnNuevoActionPerformed

    private void mnEditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnEditarActionPerformed
        SeleccionarRegistro();
    }//GEN-LAST:event_mnEditarActionPerformed

    private void mnNuevoMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_mnNuevoMouseClicked
        intOpcion = NUEVO;
        limpiarText();
        mnNuevo.setEnabled(false);
        mnGrabar.setEnabled(true);
        mnEditar.setEnabled(false);
    }//GEN-LAST:event_mnNuevoMouseClicked

    private void mnGrabarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_mnGrabarMouseClicked

        if (intOpcion == NUEVO) {
        if (rbtnPersona.isSelected()){
        GestorCliente.add(new Persona(0,
                                      Integer.parseInt(txtNroDoc.getText()),
                                      txtApellidoPat.getText(),
                                      txtApellidoMat.getText(),
                                      txtNombre.getText(),
                                      Date.valueOf(txtFechaNac.getText()),
                                      cmbSexo.getSelectedItem().toString().charAt(0),
                                      txtDireccion.getText(),
                                      txtEmail.getText(),
                                      Integer.parseInt(txtTelefono.getText())));


         }else{
               GestorCliente.add(new Empresa(0,
                                      Integer.parseInt(txtNroDoc.getText()),
                                      txtNombre.getText(),
                                      txtWeb.getText(),
                                      txtDireccion.getText(),
                                      txtEmail.getText(),
                                      Integer.parseInt(txtTelefono.getText())));

         }
        }
        else {
        if (rbtnPersona.isSelected()){
        GestorCliente.update(new Persona(Integer.parseInt(txtCodigo.getText()),
                                      Integer.parseInt(txtNroDoc.getText()),
                                      txtApellidoPat.getText(),
                                      txtApellidoMat.getText(),
                                      txtNombre.getText(),
                                      Date.valueOf(txtFechaNac.getText()),
                                      cmbSexo.getSelectedItem().toString().charAt(0),
                                      txtDireccion.getText(),
                                      txtEmail.getText(),
                                      Integer.parseInt(txtTelefono.getText())));

         }else {
               GestorCliente.update(new Empresa(Integer.parseInt(txtCodigo.getText()),
                                      Integer.parseInt(txtNroDoc.getText()),
                                      txtNombre.getText(),
                                      txtWeb.getText(),
                                      txtDireccion.getText(),
                                      txtEmail.getText(),
                                      Integer.parseInt(txtTelefono.getText())));
         }

        }
        mnGrabar.setEnabled(false);
        mnNuevo.setEnabled(true);
        mnEditar.setEnabled(true);
        refrescarGrid();
        limpiarText();

    }//GEN-LAST:event_mnGrabarMouseClicked

    private void mnEditarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_mnEditarMouseClicked
        
        SeleccionarRegistro();


    }//GEN-LAST:event_mnEditarMouseClicked

    private void mnEliminarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_mnEliminarMouseClicked
                GestorCliente.delete((Integer.parseInt(txtCodigo.getText())));
                refrescarGrid();
                limpiarText();
    }//GEN-LAST:event_mnEliminarMouseClicked
    private void SeleccionarRegistro(){
        int filSel = tblClientes.getSelectedRow();
        if (filSel != -1){
                intOpcion = MODIFICAR;
                mnEditar.setEnabled(false);
                mnNuevo.setEnabled(false);
                mnGrabar.setEnabled(true);
                mnEliminar.setEnabled(true);
                limpiarText();
                Cliente c = (Cliente) model.lista.get(filSel);
                txtCodigo.setText("" + c.getCodigo());
                txtDireccion.setText(c.getDireccion());
                txtEmail.setText(c.getEmail());
                txtTelefono.setText("" + c.getTelefono());
                if (c instanceof Persona){
                    rbtnPersona.setSelected(true);
                    rbtnEmpresa.setSelected(false);
                    Persona p = (Persona)c;
                    txtNombre.setText(p.getNombre());
                    txtApellidoPat.setText(p.getApellidoPat());
                    txtApellidoMat.setText(p.getApellidoMat());
                    txtFechaNac.setText(p.getFechaNac().toString());
                    if (p.getSexo() == 'F')
                       cmbSexo.setSelectedIndex(0);
                    else if (p.getSexo() == 'M')
                        cmbSexo.setSelectedIndex(1);
                    txtNroDoc.setText(""+p.getDni());
                } else
                {
                    rbtnPersona.setSelected(false);
                    rbtnEmpresa.setSelected(true);
                    Empresa e = (Empresa)c;
                    txtNombre.setText(e.getRazonSocial());
                    txtNroDoc.setText("" +e.getRuc());
                    txtWeb.setText(e.getPaginaWeb());
                    cmbSexo.setSelectedIndex(-1);
                    txtFechaNac.setText("");
                }
        }

}

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup btnGroup;
    private javax.swing.JComboBox cmbSexo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable1;
    private javax.swing.JMenu mnEditar;
    private javax.swing.JMenu mnEliminar;
    private javax.swing.JMenu mnGrabar;
    private javax.swing.JMenu mnNuevo;
    private javax.swing.JRadioButton rbtnEmpresa;
    private javax.swing.JRadioButton rbtnPersona;
    private javax.swing.JTable tblClientes;
    private javax.swing.JTextField txtApellidoMat;
    private javax.swing.JTextField txtApellidoPat;
    private javax.swing.JTextField txtCodigo;
    private javax.swing.JTextField txtDireccion;
    private javax.swing.JTextField txtEmail;
    private javax.swing.JTextField txtFechaNac;
    private javax.swing.JTextField txtNombre;
    private javax.swing.JTextField txtNroDoc;
    private javax.swing.JTextField txtTelefono;
    private javax.swing.JTextField txtWeb;
    // End of variables declaration//GEN-END:variables
MiTableModel model = new MiTableModel();

    public void refrescarGrid(){
        model.lista = GestorCliente.queryAll();
        tblClientes.setModel(model);
        model.fireTableChanged(null);
    }
    public void limpiarText(){
        txtCodigo.setText("");
        txtNombre.setText("");
        txtApellidoMat.setText("");
        txtApellidoPat.setText("");
        txtDireccion.setText("");
        txtEmail.setText("");
        txtFechaNac.setText("");
        txtTelefono.setText("");
        txtWeb.setText("");
        txtNroDoc.setText("");

    }
    class MiTableModel extends AbstractTableModel{
        ArrayList lista = new ArrayList();
        String [] titulos = {"Codigo", "Nombre", "Nrodoc"};

        public int getRowCount() {
            return lista.size();
        }

        public int getColumnCount() {
            return titulos.length;
        }
        public String getColumnName(int column){
            String strTitle = new String();
            if (column < titulos.length){
            strTitle= titulos[column];
            }
            else {
                strTitle ="";
             }
            return strTitle;
        }

        public Object getValueAt(int fil, int col) {
            String valor = "";
            Cliente c = (Cliente)lista.get(fil);
            if (c instanceof Persona){
            switch(col){
                case 0: valor = "" + ((Persona)lista.get(fil)).getCodigo(); break;
                case 1: valor = ((Persona)lista.get(fil)).getNombre(); break;
                case 2: valor = "" + ((Persona)lista.get(fil)).getDni(); break;
            }
            }
            else {

                switch(col){
                case 0: valor = "" + ((Empresa)lista.get(fil)).getCodigo(); break;
                case 1: valor = ((Empresa)lista.get(fil)).getRazonSocial(); break;
                case 2: valor = "" + ((Empresa)lista.get(fil)).getRuc(); break;

            }
            }
            return valor;
        }

    }
}
